<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>NovaCube - Inscription</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
	<!-- Optional theme -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
	<!-- Font awesome pour les icones -->
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
	
</head>

<body style="background-image: url('fond.png');background-repeat: no-repeat;background-attachment: fixed;background-position: center;">

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="">NovaCube</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="index.php"><i class="fa fa-home"></i> Accueil</a></li>
        <li><a href="http://novacube.fr/forums"><i class="fa fa-comments"></i> Forum</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="container-fluid">

<div class="row">
  <div class="col-md-1">
  </div>

  <div class="col-md-10">
    <div class="panel panel-default">
        <div class="panel-heading"><b><i>Pannel admin - Inscription d'utilisateurs:</i></b></div>
        <div class="panel-body">
          <form method="post" action="sign.php" class="form-horizontal">
      <div class="form-group">
        <label class="col-sm-2 control-label">Pseudo:</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" name="username" placeholder="Pseudo">
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label">Mot de passe:</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" name="password" placeholder="Mot de passe" width="20%">
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label">Répétez le mdp:</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" name="repeatpassword" placeholder="Répétez le mot de passe" width="20%">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button name="submit"type="submit" class="btn btn-default" href="#">Valider</button>
        </div>
      </div>
    </form>
        </div>
        </div>
    </div>
    <div class="col-md-1">
    </div>

  </div>

</div>

<div class="container">
<br />
<?php

if(isset($_POST['submit'])){
	$username = strtolower(htmlspecialchars(trim($_POST['username'])));
	$password = htmlspecialchars(trim($_POST['password']));
	$repeatpassword = htmlspecialchars(trim($_POST['repeatpassword']));

  
	
  if($username && $password && $repeatpassword){
    if(strlen($username) >= 4){
      if (strlen($password) >= 6) {
        if($password == $repeatpassword){
          
          $password = md5($password);

          function connexion_bd(){
          $nom_du_serveur ="sql31.free-h.org:3306";
          $nom_de_la_base ="BDDLioche";
          $nom_utilisateur ="Lioche";
          $passe ="bddliochebukkit";

          @$link = mysqli_connect($nom_du_serveur,$nom_utilisateur,$passe,$nom_de_la_base);
          @mysqli_set_charset($link,"utf8");

          if (mysqli_connect_errno($link)) {
              echo "Echec lors de la connexion à MySQL : " . mysqli_connect_error($link);
              exit();
          }

          return $link;
        }

        $connexion = connexion_bd();
        $log = mysqli_query($connexion, "SELECT * FROM users WHERE username='$username'");
        $rows = mysqli_num_rows($log);

        if($rows == 1){
        ?>
        <center>
        <div class="col-md-3"></div>
        <div class="col-md-6">
          <div class="alert alert-danger" role="alert">
            <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
            <span class="sr-only">Error:</span>
            <b>Nom d'utilisateur déjà enregistré !</b>
          </div>
        </div>
        </center>
        <?php
        }else{
          $query = mysqli_query($connexion, "INSERT INTO users VALUES('', '$username', '$password')");
          header('Location:login.php');
        }

        }else{
        ?>
        <center>
        <div class="col-md-3"></div>
        <div class="col-md-6">
          <div class="alert alert-danger" role="alert">
            <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
            <span class="sr-only">Error:</span>
            <b>Les mots de passe de correspondent pas !</b>
          </div>
        </div>
        </center>
        <?php
        }
      }else{
      ?>
      <center>
      <div class="col-md-3"></div>
      <div class="col-md-6">
        <div class="alert alert-danger" role="alert">
          <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
          <span class="sr-only">Error:</span>
          <b>Veuillez saisir un mot de passe d'au moins 6 caractères !</b>
        </div>
      </div>
      </center>
      <?php
      }
    }else{
    ?>
    <center>
    <div class="col-md-3"></div>
    <div class="col-md-6">
      <div class="alert alert-danger" role="alert">
        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
        <span class="sr-only">Error:</span>
        <b>Veuillez saisir un pseudo d'au moins 4 caractères !</b>
      </div>
    </div>
  </center>
    <?php
    }
  }else{
?>
<center>
  <div class="col-md-3"></div>
  <div class="col-md-6">
    <div class="alert alert-danger" role="alert">
      <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
      <span class="sr-only">Error:</span>
      Veuillez remplir tout les champs !
    </div>
  </div>
</center>
<?php
  }
}
?>
</div>
</body>
</html>